import { LandUseTypeAnalysisParams, ReportParagraphBlock } from "./typings/index";
import {
    isEmpty, getCellValueTotalByColGroup, getPercent, presetWorkSheet,
    getTableMatrixRowTotalValueMax, generateWordParagraBlockByPlainText,
    removeAndAdjustColumn,
    generateWordParagraBlockByTableGenerateResult,
    processWordParagraphBlockList
} from "./utils";

const managementOrganizationAndPersonnel = (params: LandUseTypeAnalysisParams): ReportParagraphBlock[] => {
    let reportParagraphBlockList: ReportParagraphBlock[] = [];
    console.log("managementOrganizationAndPersonnel--->params", params)
    try {
        const {
            workSheet,
        } = params;
        reportParagraphBlockList.push(generateWordParagraBlockByPlainText('自然保护地管理队伍', 'heading1'))
        const newWorkSheet = presetWorkSheet(workSheet);
        const protectlandName = '保护地';
        let workSheet4 = [...newWorkSheet].splice(2, newWorkSheet.length - 1);
        const totalPersonNum = Number(workSheet4[0][1]) + Number(workSheet4[0][1] + workSheet4[0][7] + workSheet4[0][12]);
        let tempParagraph4_1 = `目前，${protectlandName}共有在职工${totalPersonNum}人，包括`;
        workSheet4.forEach((rowList, rowIndex) => {
            if (rowIndex > 0) {
                if (rowIndex < workSheet4.length - 1) {
                    tempParagraph4_1 += `${rowList[0]}${rowList[1]}人，`
                } else {
                    tempParagraph4_1 += `${rowList[0]}${rowList[1]}人。`
                }
            }
        })
        tempParagraph4_1 += `按照文化结构划分，${protectlandName}硕士以上人员${workSheet4[0][2]}人，`;
        if (!isEmpty(workSheet4[0][3])) {
            tempParagraph4_1 += `本科人员${workSheet4[0][3]}人，`;
        }
        if (!isEmpty(workSheet4[0][3])) {
            tempParagraph4_1 += `专科人员${workSheet4[0][4]}人，`;
        }
        if (!isEmpty(workSheet4[0][3])) {
            tempParagraph4_1 += `中专或高中${workSheet4[0][3]}人，`;
        }
        if (!isEmpty(workSheet4[0][3])) {
            tempParagraph4_1 += `初中及以下${workSheet4[0][3]}人；`;
        }
        tempParagraph4_1 += `按照职称结构划分，${protectlandName}高级职工${workSheet4[0][8]}人，`;
        if (!isEmpty(workSheet4[0][9])) {
            tempParagraph4_1 += `中级职工${workSheet4[0][9]}人，`;
        }
        if (!isEmpty(workSheet4[0][10])) {
            tempParagraph4_1 += `助工${workSheet4[0][10]}人，`;
        }
        if (!isEmpty(workSheet4[0][11])) {
            tempParagraph4_1 += `技术员${workSheet4[0][11]}人，`;
        }
        tempParagraph4_1 += `${protectlandName}正式职工${workSheet4[0][13]}人，`;
        if (!isEmpty(workSheet4[0][14])) {
            tempParagraph4_1 += `临时工${workSheet4[0][14]}人，`;
        }
        if (!isEmpty(workSheet4[0][15])) {
            tempParagraph4_1 += `退休人员${workSheet4[0][15]}人。`;
        }
        let tableDataForRenYuan = [...workSheet];
        let columnTotalAreaIsEmptyOrNullIndexListForRenYuan: number[] = [];
        let tableRowSpanListForRenYuan = [
            { rowIndex: 0, colIndex: 0, rowSpan: 2 },
            { rowIndex: 0, colIndex: 15, rowSpan: 2 },
        ];
        let tableColSpanListForRenYuan = [
            { rowIndex: 0, colIndex: 1, colSpan: 6 },
            { rowIndex: 0, colIndex: 7, colSpan: 5 },
            { rowIndex: 0, colIndex: 12, colSpan: 3 },
        ];
        for (let i = 0; i < 15; i++) {
            if (i > 1) {
                let tempColumnTotalValue = getCellValueTotalByColGroup(workSheet4, i, 0, workSheet4.length - 1);
                if (tempColumnTotalValue == 0 || isEmpty(tempColumnTotalValue)) {
                    columnTotalAreaIsEmptyOrNullIndexListForRenYuan.push(i);
                }
            }
        }
        columnTotalAreaIsEmptyOrNullIndexListForRenYuan.forEach((columnIndex, _index) => {
            tableDataForRenYuan = removeAndAdjustColumn(tableDataForRenYuan, columnIndex - _index, tableRowSpanListForRenYuan, tableColSpanListForRenYuan)
        })
        reportParagraphBlockList.push(generateWordParagraBlockByPlainText(tempParagraph4_1, 'normalParagraph'))
        reportParagraphBlockList.push(generateWordParagraBlockByPlainText(`表  ${protectlandName}管理局（处）人员现状统计表`, 'smallTableName'))
        tableDataForRenYuan.forEach(row => {
            row.forEach(str => {
                str = str + ""
            })
        })
        reportParagraphBlockList.push(generateWordParagraBlockByTableGenerateResult({
            tableStyle: 'normalTable',
            tableData: tableDataForRenYuan,
            tableHeaderRowNumber: 1,
            tableRowSpanInfoList: tableRowSpanListForRenYuan,
            tableColSpanInfoList: tableColSpanListForRenYuan
        }))
        return processWordParagraphBlockList(reportParagraphBlockList);
    } catch (e) {
        // console.log("managementOrganizationAndPersonnel---err", e)
        return reportParagraphBlockList;
    }
}

export default managementOrganizationAndPersonnel;